/*
 * TAN-Manager
 * Copyright (C) 2011 Bernhard Bermeitinger
 * E-Mail: bernhard.bermeitinger@googlemail.com
 * Link:   https://code.google.com/p/tan-management/
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package tan_manager.gui.newfile;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

import tan_manager.Constants;
import tan_manager.datahandler.FileManager;

public class CreateNewFileFrame extends JFrame {

    /**
     * 
     */
    private static final long serialVersionUID = 8261327754667613172L;

    private JPanel wholeFrame = new JPanel(new BorderLayout());

    private JPanel header = new JPanel(new FlowLayout());
    private JTextField tanCountField = new JTextField("");
    private JButton newFileButton = new JButton(Constants.textbundle.getString("NEW_FILE"));

    private JPanel tanTable = new JPanel();
    private JTextField[] tanFields;

    private JPanel createFilePanel = new JPanel(new FlowLayout());
    private JButton createFileButton = new JButton(Constants.textbundle.getString("CREATE_FILE"));

    public CreateNewFileFrame() {
        super(Constants.textbundle.getString("CREATE_NEW_TAN_FILE"));

        tanCountField.setPreferredSize(new Dimension(80, 25));

        newFileButton.addActionListener(new NewFileListener());

        header.add(new JLabel(Constants.textbundle.getString("HOW_MANY_TANS")));
        header.add(tanCountField);
        header.add(newFileButton);

        createFileButton.addActionListener(new CreateFileListener());
        createFileButton.setEnabled(false);

        createFilePanel.add(createFileButton);

        wholeFrame.add(header, BorderLayout.NORTH);
        wholeFrame.add(tanTable, BorderLayout.CENTER);
        wholeFrame.add(createFilePanel, BorderLayout.SOUTH);

        this.add(wholeFrame);
        this.pack();
    }

    private class CreateFileListener implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent e) {
            String file = JOptionPane.showInputDialog(Constants.textbundle.getString("ENTER_BANKNAME"));
            try {
                if (!file.endsWith(Constants.FILE_ENDING)) {
                    file += Constants.FILE_ENDING;
                }
                FileManager fileManager = new FileManager(file);
                StringBuffer toWrite = new StringBuffer();
                for (JTextField jt : tanFields) {
                    toWrite.append(jt.getText());
                    toWrite.append("\n");
                }
                fileManager.write(toWrite.toString());
            } catch (SecurityException e1) {
                e1.printStackTrace();
            } catch (IOException e1) {
                e1.printStackTrace();
            }
            createFileButton.setEnabled(false);
        }
    }

    private class NewFileListener implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent ae) {
            Integer count = null;
            try {
                count = Integer.parseInt(tanCountField.getText());
            } catch (NumberFormatException e1) {
                JOptionPane.showMessageDialog(null, Constants.textbundle.getString("NO_VALID_NUMBER"),
                        Constants.textbundle.getString("ILLEGAL_NUMBER_TITLEBAR"), JOptionPane.ERROR_MESSAGE);
            }

            if (count != null) {

                tanFields = new JTextField[count];

                // delete all entries if a new file wants to be created
                for (Component c : tanTable.getComponents()) {
                    tanTable.remove(c);
                }

                tanTable.setLayout(new GridLayout(count / 5, 5));
                for (int i = 0; i < count; ++i) {
                    JLabel number = new JLabel("#" + (i+1));
                    number.setHorizontalAlignment(JLabel.RIGHT);
                    tanFields[i] = new JTextField();

                    tanTable.add(number);
                    tanTable.add(tanFields[i]);
                }
                wholeFrame.add(tanTable, BorderLayout.CENTER);
                pack();
                setSize(600, getHeight());
                createFileButton.setEnabled(true);
            }
        }
    }
}


